package library.controller;

import library.po.User;
import library.service.UserService;
import library.service.impl.UserServiceImpl;
import view.CURD.UserDialog;
import view.main.MainFrame;
import view.login.LoginDialog;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import static java.lang.System.out;

public class LoginListener implements ActionListener {
    private String userId;
    private String password;
    private UserService userService = new UserServiceImpl();
    private LoginDialog login;
    private JTextField idInput,passwordInput;
    private UserDialog ud;
    private MainFrame mainFrame;
    private int type;
    public LoginListener(LoginDialog loginDialog, JTextField idInput, JPasswordField passwordInput) {
        this.passwordInput = passwordInput;
        this.idInput = idInput;
        this.login = loginDialog;
    }
    public LoginListener(){
        super();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
       //设置身份为借阅者
        if("Reader".equals(e.getActionCommand())){
            type = 1;
        }
        //设置身份为图书管理员
        else if("BookAdmin".equals(e.getActionCommand())){
            type = 2;
        }
        //设置身份为系统管理员
        else if("SysAdmin".equals(e.getActionCommand())){
            type = 3;
        }
        //登录判断
        else if("Login".equals(e.getActionCommand())) {
            userId = idInput.getText().trim();
            password = passwordInput.getText().trim();
            userService = new UserServiceImpl();
            User toolUser1, toolUser2, unfinishedUser, superUser;
            unfinishedUser = new User(userId, password, type);
            superUser = new User(userId, password, 4);
            toolUser1 = userService.login(unfinishedUser);
            if (toolUser1 == null) {
                JOptionPane.showMessageDialog(login, "请检查用户名或密码", "用户名或密码错误", JOptionPane.ERROR_MESSAGE);
            } else {
                toolUser1 = userService.judgeIdentity(unfinishedUser);
                toolUser2 = userService.judgeIdentity(superUser);
                //打开图书借阅界面
                switch (type) {
                    case (1): {
                        if (toolUser1 != null) {
                            mainFrame = new MainFrame(toolUser1);
                            JOptionPane.showMessageDialog(login, "欢迎登录图书管理系统！", "欢迎", JOptionPane.PLAIN_MESSAGE);
                            mainFrame.setVisible(true);
                            mainFrame.setUser(toolUser1);
                            mainFrame.readerLogin();
                            login.dispose();break;
                        }
                        if (toolUser2 != null) {
                            mainFrame = new MainFrame(toolUser2);
                            JOptionPane.showMessageDialog(login, "欢迎登录图书管理系统！", "欢迎", JOptionPane.PLAIN_MESSAGE);
                            mainFrame.setVisible(true);
                            mainFrame.setUser(toolUser2);
                            mainFrame.readerLogin();
                            login.dispose();break;
                        }
                    }
                    case (2): {
                        //打开图书管理界面
                        if (type == 2 && toolUser1 != null) {
                            mainFrame = new MainFrame(toolUser1);
                            JOptionPane.showMessageDialog(login, "欢迎登录图书管理系统！", "欢迎", JOptionPane.PLAIN_MESSAGE);
                            mainFrame.setUser(toolUser1);
                            mainFrame.setVisible(true);
                            mainFrame.bookAdminLogin();
                            login.dispose();break;
                        }
                        if (type == 2 && toolUser2 != null) {
                            mainFrame = new MainFrame(toolUser2);
                            JOptionPane.showMessageDialog(login, "欢迎登录图书管理系统！", "欢迎", JOptionPane.PLAIN_MESSAGE);
                            mainFrame.setUser(toolUser2);
                            mainFrame.setVisible(true);
                            mainFrame.bookAdminLogin();
                            login.dispose();break;
                        }
                    }
                    case (3): {
                        //打开系统管理界面
                        if (type == 3 && toolUser1 != null) {
                            mainFrame = new MainFrame(toolUser1);
                            JOptionPane.showMessageDialog(login, "欢迎登录图书管理系统！", "欢迎", JOptionPane.PLAIN_MESSAGE);
                            mainFrame.setUser(toolUser1);
                            mainFrame.setVisible(true);
                            mainFrame.sysAdminLogin();
                            login.dispose();break;
                        }
                        if (type == 3 && toolUser2 != null) {
                            mainFrame = new MainFrame(toolUser2);
                            JOptionPane.showMessageDialog(login, "欢迎登录图书管理系统！", "欢迎", JOptionPane.PLAIN_MESSAGE);
                            mainFrame.setUser(toolUser2);
                            mainFrame.setVisible(true);
                            mainFrame.sysAdminLogin();
                            login.dispose();break;
                        } else {
                            JOptionPane.showMessageDialog(login, "身份不符!", "登录失败", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                }
            }
        }
        else if("Create".equals(e.getActionCommand())){
            ud = new UserDialog(login,"注册",null);
            ud.closeCurrentDialog();
        }
        //新增提交
        else if("userAddPost".equals(e.getActionCommand())) {
            User user;
            user = getUserInstance();
            if(user != null) {
                userService.insertSelective(user);
                JOptionPane.showMessageDialog(ud, "注册成功！");
                ud.dispose();
            }else {
                JOptionPane.showMessageDialog(ud, "注册失败！请检查各项信息是否符合要求！");
            }
        }
//        else if ("")
    }
    public User getUserInstance() {
        String userId = null;
        String userName = null;
        String password = null;
        String sex = null;
        Integer type = 0;
        Integer credit = 100;
        User user = null;
        /*获取账号*/
        String userIdInput  = ud.getUserIdTextField().getText().trim();
        if(userIdInput != null && !"".equals(userIdInput) && !"用户名由20位以下字母和数字组成".equals(userIdInput)) {
            userId = userIdInput;
        }else {
            JOptionPane.showMessageDialog(ud, "用户名不能为空","输入错误",JOptionPane.ERROR_MESSAGE);
        }
        /*获取姓名*/
        String nameInput = ud.getUserNameTextField().getText().trim();
        if(nameInput != null && !"".equals(nameInput) && !"您的姓名".equals(nameInput)) {
            userName = nameInput;
        }else {
            JOptionPane.showMessageDialog(ud, "姓名不能为空","输入错误",JOptionPane.ERROR_MESSAGE);
        }
        /*获取密码*/
        String passwordInput = ud.getPasswordField().getText().trim();
        if(passwordInput != null && !"".equals(passwordInput)) {
            password = passwordInput;
        }
        else {
            JOptionPane.showMessageDialog(ud, "密码不能为空","输入错误",JOptionPane.ERROR_MESSAGE);
        }
        /*获取性别*/
        String sexInput = ud.getSexBox().getSelectedItem().toString();
        if(sexInput != null && !"".equals(sexInput)&& !sexInput.equals("--请选择--")) {
            sex = sexInput;
        }
        else
            JOptionPane.showMessageDialog(ud, "请选择您的性别","输入错误",JOptionPane.ERROR_MESSAGE);
        /*获取身份*/
        Integer typeInput = identityToType(ud.getTypeBox().getSelectedItem().toString());
        if(typeInput != null && !"".equals(typeInput)) {
            type = typeInput;
            out.println(userId);
            out.println(password);
            out.println(userName);
            out.println(sex);
            out.println(type);
        }else {
            JOptionPane.showMessageDialog(ud, "身份不能为空","输入错误",JOptionPane.ERROR_MESSAGE);
        }
        if(userId != null && userName != null && sex != null && type > 0 && password!=null) {
            user = new User(userId,password,userName,sex,type,credit);
        }
        return user;
    }
    public Integer identityToType(String identity){
        Integer type = 0;
        if (identity.equals("借阅者")){type = 1;}
        if (identity.equals("系统管理员&&借阅者")){type = 2;}
        if (identity.equals("图书管理员&&借阅者")){type = 3;}
        if (identity.equals("系统管理员&&图书管理员&&借阅者")){type = 4;}
        return type;
    }
    public String typeToIdentity(int type) {
        String str = null;
        if (type == 1) {
            str = "借阅者";
        }
        else if(type == 3){
            str = "图书管理员&借阅者";
        }
        else if(type == 2){
            str = "系统管理员&读者";
        }
        else if (type == 4)
            str = "系统管理员&图书管理员&借阅者";
        return str;
    }
    public void setUserAddDialog(UserDialog userAddDialog) {
        ud = userAddDialog;
    }
}
